home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / csrc1.arc / OLDMC.C < prev    next >
C/C++ Source or Header  |  1989-07-27  |  4KB  |  248 lines

  1.  
  2. /*
  3.  * mc.c
  4.  *
  5.  * Convert a file to multi column format.
  6.  *
  7.  * Usage:
  8.  *    mc [-option value]... files.
  9.  *
  10.  * Options:
  11.  *                        Default
  12.  *    -c    number of columns           2
  13.  *    -h    number of lines on the page      60
  14.  *    -w    width of the page         132
  15.  */
  16.  
  17. /*)BUILD    $(TKBOPTIONS) = {
  18.             TASK    = ...MUL
  19.         }
  20. */
  21.  
  22. #ifdef    DOCUMENTATION
  23.  
  24. title    mc    Multi-column Filter
  25. index        Multi-column Filter
  26.  
  27. synopsis
  28.  
  29.     mc [-options] file__list
  30.  
  31. description
  32.  
  33.     Mc converts its input files to a multi-column format, writing all
  34.     output to the standard output.  If no input files are specified,
  35.     the standard input is read.
  36.     .s
  37.     The following options may be used to control mc:
  38.     .lm +16
  39.     .s.i-16;-c value (  2)    Number of columns to output.
  40.     .s.i-16;-h value ( 60)    Height (lines per page).
  41.     .s.i-16;-w value (132)    Width (columns per line).
  42.     .lm -16
  43.  
  44. diagnostics
  45.  
  46.     .lm +8
  47.     .s.i -8;Unreasonable -c (value) or -w (value).
  48.     .s.i -8;Bad (option) specification.
  49.     .s.i -8;(Filename): cannot open.
  50.     .lm -8
  51.  
  52. author
  53.  
  54.     David Conroy
  55.  
  56. bugs
  57.  
  58. #endif
  59.  
  60. #include <stdio.h>
  61.  
  62. #define    WMAX    132
  63. #define HMAX    60
  64.  
  65. int    wmax    = WMAX;
  66. int    hmax    = HMAX;
  67. int    ncol    = 2;
  68.  
  69. int    row;
  70. int    col;
  71. int    lmax;
  72.  
  73. char    line[128];
  74. char    page[HMAX][WMAX];
  75.  
  76. main(argc, argv)
  77. char *argv[];
  78. {
  79.     register char *p;
  80.     register c, i;
  81.     int nf;
  82.     FILE *fp;
  83.  
  84.     nf = argc-1;
  85.     for(i=1; i<argc; ++i) {
  86.         p = argv[i];
  87.         if(*p == '-') {
  88.             argv[i] = 0;
  89.             --nf;
  90.             for(++p; c = *p++; )
  91.                 switch(c) {
  92.  
  93.                 case 'c':
  94.                 case 'C':
  95.                     if(++i >= argc)
  96.                         usage();
  97.                     ncol = atoi(argv[i]);
  98.                     if(ncol == 0)
  99.                         bad(ncol, "column");
  100.                     argv[i] = 0;
  101.                     --nf;
  102.                     break;
  103.  
  104.                 case 'h':
  105.                 case 'H':
  106.                     if(++i >= argc)
  107.                         usage();
  108.                     hmax = atoi(argv[i]);
  109.                     if(hmax > HMAX)
  110.                         bad(hmax, "height");
  111.                     argv[i] = 0;
  112.                     --nf;
  113.                     break;
  114.  
  115.                 case 'w':
  116.                 case 'W':
  117.                     if(++i >= argc)
  118.                         usage();
  119.                     wmax = atoi(argv[i]);
  120.                     if(wmax > WMAX)
  121.                         bad(wmax, "width");
  122.                     argv[i] = 0;
  123.                     --nf;
  124.                     break;
  125.  
  126.                 default:
  127.                     usage();
  128.                 }
  129.         }
  130.     }
  131.     lmax = wmax/ncol - 1;
  132.     if(lmax < 1)
  133.         error("Unreasonable -c (%d) or -w (%d).\n", ncol, wmax);
  134.     if(nf == 0)
  135.         process(stdin);
  136.     else
  137.         for(i=1; i<argc; ++i)
  138.             if(p = argv[i]) {
  139.                 if((fp=fopen(p, "r")) == NULL) {
  140.                     fprintf(stderr, "%s: cannot open.\n",
  141.                             p);
  142.                     continue;
  143.                 }
  144.                 process(fp);
  145.                 fclose(fp);
  146.             }
  147. }
  148.  
  149. process(fp)
  150. FILE *fp;
  151. {
  152.     register char *p1, *p2;
  153.     register c;
  154.  
  155.     row = 0;
  156.     col = 0;
  157.     blank();
  158.     while(get(fp)) {
  159.         if(++row >= hmax) {
  160.             if(++col >= ncol) {
  161.                 output();
  162.                 col = 0;
  163.                 blank();
  164.             }
  165.             row = 0;
  166.         }
  167.         p1 = line;
  168.         p2 = &page[row][(wmax*col)/ncol];
  169.         while(c = *p1++)
  170.             *p2++ = c;
  171.     }
  172.     output();
  173. }
  174.  
  175. output()
  176. {
  177.     register char *p1, *p2;
  178.     register i;
  179.  
  180.     if(row==0 && col==0)
  181.         return;
  182.     putchar('\f');
  183.     for(i=0; i<hmax; ++i) {
  184.         p1 = &page[i][0];
  185.         p2 = p1 + wmax;
  186.         while(p2>p1 && p2[-1]==' ')
  187.             --p2;
  188.         while(p1 < p2)
  189.             putchar(*p1++);
  190.         putchar('\n');
  191.     }
  192. }
  193.  
  194. blank()
  195. {
  196.     register char *p1, *p2;
  197.     register i;
  198.  
  199.     for(i=0; i<hmax; ++i) {
  200.         p1 = &page[i][0];
  201.         p2 = p1 + wmax;
  202.         while(p1 < p2)
  203.             *p1++ = ' ';
  204.     }
  205. }
  206.  
  207. get(fp)
  208. FILE *fp;
  209. {
  210.     register char *p;
  211.     register c, h;
  212.  
  213.     p = line;
  214.     h = 0;
  215.     while((c=getc(fp))!=EOF && c!='\n') {
  216.         if(c == '\b') {
  217.             if(h) {
  218.                 --p;
  219.                 --h;
  220.             }
  221.         } else if(c == '\t') {
  222.             do {
  223.                 *p++ = ' ';
  224.             } while((++h&07) != 0);
  225.         } else if(c>=' ' && c<='~') {
  226.             *p++ = c;
  227.             ++h;
  228.         }
  229.     }
  230.     *p = 0;
  231.     line[lmax] = 0;
  232.     return(c == '\n');
  233. }
  234.  
  235. bad(n, s)
  236. char *s;
  237. {
  238.     error("%d: bad %s specification.\n", n, s);
  239. }
  240.  
  241. usage()
  242. {
  243.     error("Usage: mc [-c #] [-h #] [-w #] [file ...].\n\
  244.     -c Number of columns          <2>\n\
  245.     -h Height (lines per page)   <60>\n\
  246.     -w Width (columns per line) <132>\n");
  247. }
  248.